HotPanel = function() {
	
	this.panel = new Ext.Panel({
			id: 'hotpanle-win',
			title: '排行帮',
			width:250,
			height:400,
			x : 100,
			y : 150,
			iconCls: 'accordion',
			shim:false,
			animCollapse:false,
			constrainHeader:true,

			layout:'accordion',
			border:false,
			layoutConfig: {
					animate:false
			},

			items: [
					new Ext.tree.TreePanel({
							id:'hotpanel-tree',
							title: '行政区',
							rootVisible:false,
							lines:false,
							autoScroll:true,
							root: new Ext.tree.AsyncTreeNode({
								text: '管理',
								cls:'feeds-node',
								expanded:true, 
								children :[
									{	text: '房源统计报表',	
										iconCls:'tasks',
										expanded : true,
										children :[
											{text: '房源[员工.日报]', leaf:true, url:'fy.yg.rb'},
											{text: '房源[员工.周报]', leaf:true, url:'fy.yg.zb'},
											{text: '房源[员工.月报]', leaf:true, url:'fy.yg.yb'},
											{text: '房源[员工.交易]', leaf:true, url:'fy.yg.jy'},
											{text: '房源[员工.状态]', leaf:true, url:'fy.yg.zt'},
											{text: '房源[员工.公私]', leaf:true, url:'fy.yg.gs'},
											{text: '房源[员工.委托]', leaf:true, url:'fy.yg.wt'},
											{text: '房源[员工.来源]', leaf:true, url:'fy.yg.ly'},
											{text: '房源[城区.交易]', leaf:true, url:'fy.cq.jy'}, //实验一下
											{text: '房源[城区.用途]', leaf:true, url:'fy.cq.yt'},   
											{text: '跟进[员工.日报]', leaf:true, url:'fygj.yg.rb'},
											{text: '跟进[员工.周报]', leaf:true, url:'fygj.yg.zb'},
											{text: '跟进[员工.月报]', leaf:true, url:'fygj.yg.yb'},
											{text: '跟进[员工.方式]', leaf:true, url:'fy.gj.yg.fs'},
											{text: '跟进[员工.房源]', leaf:true, url:'fy.gj.yg.fy'},
											{text: '看房[员工.日报]', leaf:true, url:'kf.yg.rb'},
											{text: '看房[员工.周报]', leaf:true, url:'kf.yg.zb'},
											{text: '看房[员工.月报]', leaf:true, url:'kf.yg.yb'},
											{text: '任务[员工.操作]', leaf:true, url:'fy.rw.yg.cz'},
											{text: '广告[员工.媒体]', leaf:true, url:'fy.gg.yg.mt'},
											{text: '资料[员工.状态]', leaf:true, url:'fy.zl.yg.zt'},
											{text: '资料[城区.状态]', leaf:true, url:'fy.zl.cq.zt'}
										]
									},
									{	text: '客源统计报表',	
										iconCls:'cop',
										expanded : true, 
										children: [
											{text: '客源[员工.日报]', leaf:true, url:'ky.yg.rb'},
											{text: '客源[员工.周报]', leaf:true, url:'ky.yg.zb'},
											{text: '客源[员工.月报]', leaf:true, url:'ky.yg.yb'},
											{text: '客源[员工.交易]', leaf:true},
											{text: '客源[员工.状态]', leaf:true},
											{text: '客源[员工.公私]', leaf:true},
											{text: '客源[员工.等级]', leaf:true},
											{text: '客源[员工.来源]', leaf:true},
											{text: '跟进[员工.日报]', leaf:true, url:'kygj.yg.rb'},
											{text: '跟进[员工.周报]', leaf:true, url:'kygj.yg.zb'},
											{text: '跟进[员工.月报]', leaf:true, url:'kygj.yg.yb'},
											{text: '跟进[员工.方式]', leaf:true},
											{text: '跟进[员工.房源]', leaf:true},
											{text: '看房[员工.日报]', leaf:true, url:'kf.yg.rb'},
											{text: '看房[员工.周报]', leaf:true, url:'kf.yg.zb'},
											{text: '看房[员工.月报]', leaf:true, url:'kf.yg.yb'},
											{text: '任务[员工.操作]', leaf:true},
											{text: '意向[员工]', leaf:true}
										]
									},
									{
										text: '合同统计报表',	
										iconCls:'feed-icon',
										expanded : false,
										children: [
											{text: '地块管理',	leaf:true},
											{text: '二调数据管理',leaf:true},
										]										
									},
									{
										text: '其它统计报表',	
										iconCls:'feed-icon',
										expanded : false,
										children: [
											{text: '地块管理',	leaf:true},
											{text: '二调数据管理',leaf:true},
										]										
									}]									
							}),
							collapseFirst:false
					}), 
					new Ext.tree.TreePanel({
							id:'hot-panel-tree2',
							title: '按时间',
							rootVisible:false,
							lines:false,
							autoScroll:true,
							tools:[{
									id:'refresh',
									on:{
											click: function(){
													var tree = Ext.getCmp('time-tree');
													tree.body.mask('Loading', 'x-mask-loading');
													tree.root.removeAll();
													tree.root.reload();
													tree.root.collapse(true, false);
													setTimeout(function(){ // mimic a server call
															tree.body.unmask();
															tree.root.expand(true, true);
													}, 1000);
											}
									}
							}],
							loader: new Ext.tree.TreeLoader({
								dataUrl: '/desktop/get_khtree',
								baseAttrs: { uiProvider: Ext.ux.TreeCheckNodeUI } 
							}),
							root: {
								nodeType: 'async',
								text: '联系人',
								draggable:false,
								id:'root'
							}
					})
			]
	});
	
	this.setDate = function(url) {
		//var d = new Date();
		//d.setMonth(d.getMonth()-month);
		//return d.format('Y-m-d');
		var date1, date2, days;
		
		switch(url)
		{
		case 'fy.yg.rb':
		case 'ky.yg.rb':
		case 'fygj.yg.rb':
		case 'kygj.yg.rb':
		case 'kf.yg.rb':
		  var d = new Date();
			days = 7;
			date2  = d.format('Y-m-d');
			d.setTime(d.getTime()-(days-1)*86400000);
			date1 = d.format('Y-m-d');
		  break;
		case 'fy.yg.zb':
		case 'ky.yg.zb':
		case 'fygj.yg.zb':
		case 'kygj.yg.zb':		
		case 'kf.yg.zb':
		  var d = new Date();
			days = 4;
			var offset = 6-d.getDay();
			d.setTime(d.getTime()+offset*86400000);
			date2  = d.format('Y-m-d');
			d.setTime(d.getTime()-(days*7-offset)*86400000);
			date1 = d.format('Y-m-d');
	  	break;
		case 'fy.yg.yb':
		case 'ky.yg.yb':
		case 'fygj.yg.yb':
		case 'kygj.yg.yb':		
		case 'kf.yg.yb':
		  var d = new Date();
			days = 6;
			date2  = d.format('Y-m-d');
			d.setTime(d.getTime()-182*86400000);
			date1 = d.format('Y-m-d');
	  	break;

		default:
		  var d = new Date();
			date2 = d.format('Y-m-d');
			date1 = d.format('Y-m-d');
			days = 1;		  
		};
		
		Ext.getCmp('start_date').setValue(date1);
		Ext.getCmp('end_date').setValue(date2);
		Ext.getCmp('days').setValue(days)
	}
	
	HotPanel.superclass.constructor.call(this, {
			id:'hot-view',
			layout:'border',
			hideMode:'offsets',
			closable:true,	 //!!add this to close this panel
			items: [{
					region:"center",
					id: "hotpanel-form",
					xtype:"panel",
	        layout:"fit",
					border: false,
					autoScroll: true,
					tbar : [
						"开始时间",
						new Ext.form.DateField({
							width:90,
							allowBlank:false,
							format:'Y-m-d',
							id: 'start_date',
							name: 'start_date',
							value: (new Date()).format('Y-m-d'),
							listeners:{
								scope: this,
								'select': function(date){
								}
					    }
						}),
						"结束时间",
						new Ext.form.DateField({
							width:90,
							allowBlank:false,
							format:'Y-m-d',
							id: 'end_date',
							name: 'end_date',
							value: (new Date()).format('Y-m-d'),
							listeners:{
								scope: this,
								'select': function(date){
								}
					    }
						}),
						//"&nbsp&nbsp&nbsp期限",
						new Ext.form.TextField({
							width:40,
							id: 'days',
							allowBlank:false,
							value:'7',
							align:'center',
							name: 'periods',
							hidden: true
						}),
						'->',
						{
							text:"重新统计",
							tooltip:'重新统计',
							iconCls:'refresh',
							handler : this.onClick
						},	
						{
							tooltip:'打印',
							iconCls:'user-add'
						}	
					],
					html: ''
				},
				{
					id:'hot-left-panel',
					layout:'fit',
					border:false,
					region:"west",
					split: true,
					width:250,
					items:this.panel
				}
				]
	});
	
	//Ext.getCmp('start_date').value = setDate(4);
	
	var hot_tree = Ext.getCmp('hotpanel-tree');
	hot_tree.getSelectionModel().on({
			'beforeselect' : function(sm, node){
					 return node.isLeaf();
			},
			'selectionchange' : function(sm, node){
				/*
					if(node){
							pars = {id:node.id};
							new Ajax.Request("/feed/get_xzq_tree_data", { 
									method: "POST",
									parameters: pars,
									onComplete:	 function(request) {
										var record = 	eval("("+request.responseText+")");	 
											
										var detailPanel = Ext.getCmp('totals-form');
										totalTpl.overwrite(detailPanel.body, record[0]);											
									}
							});
					}
				*/	
			},
			scope:this
	});
	
	hot_tree.on({
		'click' : {
		    fn: this.onClick,
		    scope: this,
		    delay: 100
		}
	})
	
};

Ext.extend(HotPanel, Ext.Panel, {
	
	onClick : function() {
		
		var hotTplMarkup = [
				'<br /><div style="text-align: center; font-size:20px; color:red; height:30px padding:5px"  >{title}</div><br />',
				'<div style="text-align:center;">',
				'<TABLE id="total_table">',
				'{body}',
				'</TABLE>',
				'</div>',
				'<br /><br />'
		];

		var hotTpl = new Ext.Template(hotTplMarkup);

		var node = Ext.getCmp('hotpanel-tree').getSelectionModel().getSelectedNode();
		if (node.isLeaf()) {
			
			if (this.id == 'hot-view')  this.setDate(node.attributes.url);
			
			var start_date = Ext.getCmp('start_date').value;
			var end_date = Ext.getCmp('end_date').value;
			var pars = {url:node.attributes.url, text:node.text, start_date:start_date, end_date:end_date};
			new Ajax.Request("/desktop/get_statistic_cq_yg", { 
					method: "POST",
					parameters: pars,
					onComplete:	 function(request) {
						var record = 	eval("("+request.responseText+")");	 
						var detailPanel = Ext.getCmp('hotpanel-form');
						hotTpl.overwrite(detailPanel.body, record);
					}
			});
		}
	}
});

Ext.reg('apphotpanel', HotPanel);